30 Lecture

CS402

Midterm & Final Term Short Notes

Context Free Grammar (CFG)

Context-free grammar (CFG) is a formalism used to describe languages in formal language theory. A CFG consists of a set of rules, also known as productions, that specify how to generate strings in a language. The rules are applied recursively to


Important Mcq's
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. Which of the following is true about context-free grammar? A) It can describe only regular languages B) It can describe only context-sensitive languages C) It can describe both regular and context-sensitive languages D) It can describe only context-free languages Answer: D What is a production rule in a context-free grammar? A) A rule that specifies how to generate a string B) A rule that specifies the terminal symbols of a language C) A rule that specifies the non-terminal symbols of a language D) A rule that specifies the start symbol of a language Answer: A Which of the following is true about a context-free grammar? A) It can generate infinite strings B) It can generate only finite strings C) It can generate both finite and infinite strings D) It cannot generate any strings Answer: A Which of the following is a non-terminal symbol in a context-free grammar? A) a B) b C) A D) B Answer: C and D What is the purpose of the start symbol in a context-free grammar? A) It specifies the first production rule to be applied B) It specifies the last production rule to be applied C) It specifies the non-terminal symbols of a language D) It specifies the beginning of a string generated by the grammar Answer: D Which of the following is true about leftmost and rightmost derivations in a context-free grammar? A) They always produce the same parse tree B) They always produce different parse trees C) They can produce the same or different parse trees D) They cannot produce parse trees Answer: A Which of the following is true about a parse tree generated by a context-free grammar? A) It shows the order in which the production rules were applied B) It shows the terminal symbols of the language C) It shows the non-terminal symbols of the language D) It shows the start symbol of the language Answer: A, B, and C What is the Chomsky normal form of a context-free grammar? A) A form in which every production rule has only one non-terminal symbol on the right-hand side B) A form in which every production rule has at most two non-terminal symbols on the right-hand side C) A form in which every production rule has only one terminal symbol on the right-hand side D) A form in which every production rule has at most two terminal symbols on the right-hand side Answer: B Which of the following is true about the pumping lemma for context-free languages? A) It is used to prove that a language is context-free B) It is used to prove that a language is not context-free C) It is used to prove that a language is regular D) It is used to prove that a language is not regular Answer: B Which of the following is an example of a context-free language? A) {anbn : n ? 0} B) {anbn : n ? 1} C) {anbmck : n, m, k ? 0} D) {anbm : n, m ? 0} Answer: C


Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is a context-free grammar, and how is it used to generate a language? Answer: A context-free grammar is a formal notation for describing the rules for generating a language. It consists of a set of production rules that specify how to derive the strings of a language. To generate a language using a context-free grammar, we start with a start symbol, and we repeatedly apply the production rules until we obtain a string of terminal symbols that belongs to the language.

  2. What is the difference between a terminal symbol and a non-terminal symbol in a context-free grammar? Answer: A terminal symbol is a symbol that cannot be further derived in a context-free grammar. It represents an element of the language being generated. A non-terminal symbol, on the other hand, is a symbol that can be further derived using the production rules of the grammar. It represents a category of elements that can be generated by the grammar.

  3. What is a parse tree in the context of context-free grammars? Answer: A parse tree is a graphical representation of the derivation of a string in a context-free grammar. It shows the order in which the production rules were applied to generate the string. The tree has the start symbol at the root, and the leaves represent the terminal symbols of the string.

  4. What is a leftmost derivation in a context-free grammar? Answer: A leftmost derivation is a type of derivation in which the leftmost non-terminal symbol in the current string is always replaced by its corresponding production rule. This type of derivation is useful for constructing parse trees from the grammar.

  5. What is the Chomsky normal form of a context-free grammar? Answer: The Chomsky normal form is a standard form for context-free grammars in which every production rule has at most two non-terminal symbols on the right-hand side. This form is useful for simplifying the grammar and for certain types of analyses of the grammar.

  6. What is the pumping lemma for context-free languages? Answer: The pumping lemma for context-free languages is a tool for proving that a language is not context-free. It states that if a language is context-free, then any string in the language of sufficient length can be divided into three parts, such that the middle part can be repeated any number of times and the resulting string is still in the language.

  7. What is ambiguity in the context of context-free grammars? Answer: Ambiguity occurs when a string in a language can be generated by more than one parse tree. This can lead to difficulties in parsing and understanding the meaning of the string.

  8. What is the difference between a context-free language and a regular language? Answer: A context-free language is a language that can be generated by a context-free grammar, while a regular language is a language that can be generated by a regular grammar. Regular grammars are less powerful than context-free grammars, and can generate only a subset of the languages that context-free grammars can generate.

  9. What is the purpose of the start symbol in a context-free grammar? Answer: The start symbol is a non-terminal symbol that represents the entire language being generated by the grammar. It is used as the initial symbol in the derivation process.

  10. What is a derivation tree, and how is it used to analyze a context-free grammar? Answer: A derivation tree is a tree that represents the derivation of a string in a context-free grammar. It shows the sequence of production rules that were applied to generate the string. Derivation trees can be used to analyze the properties of the grammar, such as ambiguity and the existence of certain types of structures in the language.

Context-free grammar (CFG) is a type of formal grammar that describes a formal language. It consists of a set of production rules that generate a language by recursively rewriting symbols. CFGs are widely used in programming language design, natural language processing, and other areas of computer science. Some of the key concepts in CFG include:
  • Non-terminal symbols: These are the symbols that can be rewritten by production rules. They represent sets of strings that can be generated by the grammar.
  • Terminal symbols: These are the symbols that cannot be rewritten by production rules. They represent the basic elements of the language.
  • Production rules: These rules specify how non-terminal symbols can be rewritten in terms of other symbols. They consist of a non-terminal symbol on the left-hand side and a string of symbols (including non-terminals and terminals) on the right-hand side.
  • Start symbol: This is the non-terminal symbol that represents the entire language generated by the grammar.
To define a CFG, we need to specify the set of non-terminal symbols, the set of terminal symbols, the start symbol, and the set of production rules. A simple example of a CFG is: S ? aSb | ? This grammar generates the language of all strings of the form a^n b^n, where n ? 0. The non-terminal symbol S is the start symbol, and the two production rules specify how to rewrite S. The symbol ? represents the empty string. CFGs can be analyzed using parsing algorithms, which determine whether a given string belongs to the language generated by the grammar. Some commonly used parsing algorithms for CFGs include recursive descent parsing, LL parsing, and LR parsing. Overall, CFGs provide a powerful framework for describing formal languages and are widely used in various fields of computer science.